package pers.vinsonws.jianghu.clientwin.ui;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @auther vinsonws
 * @date 2018/11/11 22:51
 */
public class RegisterUi extends JFrame {
    private static final long serialVersionUID = 2L;
    private JPanel mainPanel;
    private JTextField userNameTxt;
    private JPasswordField userPwdTxt;
    private JPasswordField userPwdTxtRe;
    private ImageIcon background;			//背景图
    private JLabel backgroundCon;			//背景容器



    public RegisterUi() throws HeadlessException {
        addWindowListener(new WindowAdapter() {


            public void windowClosing(WindowEvent e) {

                Info.closeChannel();
                dispose();
            }

        });
        //关闭其它界面
        Info.loginUi.setVisible(false);
        //设置主界面

        setTitle("JH-江湖");
        setSize(1176, 664);						/*设置窗口大小*/
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);			/*关闭窗口*/
        setUndecorated(false);
        //设置背景层
        background = new ImageIcon("D:\\github\\online-chat-room-netty\\jianghu-client-win\\src\\main\\resources\\img\\registerui.png");
        backgroundCon = new JLabel(background);
        backgroundCon.setBounds(0, 0,background.getIconWidth(), background.getIconHeight());
        getLayeredPane().add(backgroundCon, new Integer(Integer.MIN_VALUE));
        JPanel jp=(JPanel)getContentPane();
        jp.setOpaque(false);//设置透明



        //设置输入框位置
        mainPanel = new JPanel();
        mainPanel.setOpaque(false);
        setContentPane(mainPanel);
        mainPanel.setLayout(null);		/*使用绝对布局*/

        userNameTxt = new JTextField();
        userNameTxt.setColumns(10);
        userNameTxt.setBounds(510, 424, 150, 22);
        userNameTxt.setOpaque(false);
        userNameTxt.setBorder(null);
        mainPanel.add(userNameTxt);



        userPwdTxt = new JPasswordField();
        userPwdTxt.setColumns(10);
        userPwdTxt.setBounds(510, 474, 150, 22);
        userPwdTxt.setOpaque(false);
        userPwdTxt.setBorder(null);
        mainPanel.add(userPwdTxt);

        userPwdTxtRe = new JPasswordField();
        userPwdTxtRe.setColumns(10);
        userPwdTxtRe.setBounds(510, 524, 150, 22);
        userPwdTxtRe.setOpaque(false);
        userPwdTxtRe.setBorder(null);
        mainPanel.add(userPwdTxtRe);






        //增加注册按钮
        JButton  regBut = new JButton("注册");
        JButton  cancelBut = new JButton("退出");
        regBut.setBounds(450, 570, 120, 30);
        cancelBut.setBounds(600, 570, 120, 30);
        cancelBut.setContentAreaFilled(false);			/*异常*/
        regBut.setContentAreaFilled(false);			/*异常*/
        regBut.setIcon(new ImageIcon("images/register.png"));
        cancelBut.setIcon(new ImageIcon("images/cancel.png"));
        JButton  loginView = new JButton("登录");
        loginView.setBounds(300, 570, 120, 30);
        loginView.setContentAreaFilled(false);			/*异常*/
        mainPanel.add(regBut);
        mainPanel.add(cancelBut);
        mainPanel.add(loginView);

        //按钮交互


        loginView.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Info.registerUi.setVisible(false);
                Info.loginUi.setVisible(true);
            }
        });



        cancelBut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                Info.channel.close();
            }
        });
        regBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(UserService.register(userNameTxt.getText(),
                        String.valueOf(userPwdTxt.getPassword()),
                        String.valueOf(userPwdTxtRe.getPassword())))
                    JOptionPane.showMessageDialog(null, "注册成功！Uid为" + Info.uid);
                else JOptionPane.showMessageDialog(null, "注册失败,可能是密码不一致或者网络错误！");
            }
        });
        regBut.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar() == KeyEvent.VK_ENTER){
                    if(UserService.register(userNameTxt.getText(),
                            String.valueOf(userPwdTxt.getPassword()),
                            String.valueOf(userPwdTxtRe.getPassword())))
                        JOptionPane.showMessageDialog(null, "注册成功！Uid为" + Info.uid);
                    else JOptionPane.showMessageDialog(null, "注册失败,可能是密码不一致或者网络错误！");
                }
            }
        });
    }
}
